<template>
    <div style="height: 100%;  background-color: #f1f1f1;">
        <header class="hd">
            <span style="padding: 0 .2415rem;" @click="router.go(-1)">返回</span>
            <span>个人信息</span>
            <span style="padding: 0 .2415rem;"><el-button type="success" :icon="Check"
                    @click="goUpdateUserInfo">修改</el-button></span>
        </header>
        <div class="hidden">
            <span style="padding: 0 .2415rem;">返回</span>
            <span>个人信息</span>
            <span style="padding: 0 .2415rem;"></span>
        </div>
        <main>
            <el-card class="box-card" @click="chooseImg">
                <div class="item">
                    <input type="file" ref="photoInput" accept="image/*" style="display: none;">
                    <span class="title">头像</span>
                    <img :src="UserInfo.avatarUrl" alt="">
                </div>
            </el-card>
            <el-card class="box-card">
                <div class="item">
                    <span class="title">用户名</span>
                    <!-- <span class="contitle">{{ UserInfo.username }}</span> -->
                    <input type="text" class="contitle" v-model="UserInfo.username">
                </div>
            </el-card>
            <el-card class="box-card">
                <div class="item">
                    <span class="title">电话</span>

                    <input type="text" class="contitle" v-model="UserInfo.phone">
                </div>
            </el-card>
            <el-card class="box-card">
                <div class="item">
                    <span class="title">性别</span>

                    <input type="text" class="contitle" v-model="UserInfo.gender">
                </div>
            </el-card>
            <el-card class="box-card">
                <div class="item">
                    <span class="title">密码</span>
                    <span class="contitle">唉，就是不给你改。</span>
                </div>
            </el-card>
        </main>
         <!-- 弹窗 -->
    <div class="maske" v-if="hidden">  
    </div>
    <div class="content"  v-if="hidden">
        <span>修改成功</span>
        <span class="iconfont icon-duihao"></span>
    </div>
    </div>
   
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getUserInfo, putBase64Img, UpdateUserInfo } from '@/api/index';
import {
    Check
} from '@element-plus/icons-vue'

const route = useRoute();
const router = useRouter();
let UserInfo = ref({
});
// 修改成功的弹窗
let hidden =ref(false);
// 确认信息
let goUpdateUserInfo = async () => {
    // console.log({...UserInfo.value});
    let res = await UpdateUserInfo({ id: route.query.id, ...UserInfo.value });
    console.log(res);
    if (res.status == 200) {
        // 弹窗告诉成功
        // 跳转页面
       hidden.value=true;
        localStorage.setItem('userInfo',JSON.stringify([res.data.data]));
       router.push('/my');
    }
};
let off = ref(true);
let photoInput = ref();
// 改头像
let chooseImg = () => {
    off.value = true;
    photoInput.value.click();
    // 添加change事件监听器，当用户选择文件后触发
    photoInput.value.addEventListener('change', (e) => {
        if (off.value) {
            const file = e.target.files[0]; // 获取用户选择的文件
            const reader = new FileReader(); // 创建一个FileReader对象

            // 设置文件读取完成后的回调函数
            reader.onload = async (event) => {
                const imageUrl = event.target.result; // 获取图片的base64编码
                // 在这里可以将图片显示在页面上或进行其他操作  

                let data = await putBase64Img(new Date().getTime() + e.target.files[0].name, imageUrl)
                UserInfo.value.avatarUrl = 'https://' + data.data.imgUrl
            };
            off.value = false;
            // 读取文件内容
            reader.readAsDataURL(file);
        }

    });
};
onMounted(async () => {
    //console.log('3.-组件挂载到页面之后执行-------onMounted')
    let data = await getUserInfo(route.query.id);
    console.log(data);
    UserInfo.value = data.data.data;
})
</script>
<style scoped lang='css'>
.hd {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    z-index: 98;
    justify-content: space-between;
    font-size: .4348rem;
    background-color: #fff;
    padding: .2415rem;
    margin-bottom: .2415rem;
}

.hidden {
    /* display: none; */
    display: flex;
    justify-content: space-between;
    font-size: .4348rem;
    background-color: #fff;
    padding: .2415rem;
    margin-bottom: .2415rem;
    opacity: 0;
}

.title {
    font-size: .4831rem;
}

.item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;
}

.item input {
    padding: .2415rem;
    border: 0;
    text-align: end;
}

.item input:focus {
    /* 添加需要的样式 */
    border: 0;
    text-align: start;
    box-shadow: 0 0 5px blue;
}

.item::before {
    content: "";
    width: .3932rem;
    height: .3932rem;
    border-right: .0542rem solid #444343;
    border-bottom: .0542rem solid #444343;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%) rotate(-45deg);
}

.item img {
    width: 1.4493rem;
    height: 1.4493rem;
    border-radius: 50%;
    margin-right: .5415rem;
}

.contitle {
    font-size: .3865rem;
    margin-right: .4415rem;
}

.box-card {
    margin-bottom: .1208rem;
}

.maske {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    background-color: #c5c5c5;
    opacity: .5;
}

.content {
    z-index: 99;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background-color: #fff;
    width: 40%;
    height: 10%;
    opacity: 1;
    text-align: center;
    font-size: .4348rem;
    display: flex;
    flex-direction: column;
}
.icon-duihao {
    font-size: .7246rem;
}
</style>